#include "ET++.ph"
#include "Printer.h"

#include "Class.h"
#include "PictPort.h"
#include "Picture.h"
#include "Data.h"
#include "String.h"
#include "TypeMatcher.h"

//---- PrintPictPort -----------------------------------------------------------

class PrintPictPort: public PictPort {
	char *name;
	int pn;
public:
	MetaDef(PrintPictPort);
	PrintPictPort(const char *name);
	~PrintPictPort();
	void DevOpenPage(int pn);
	void DevClosePage();
};

NewMetaImpl0(PrintPictPort, PictPort);

PrintPictPort::PrintPictPort(const char *nm) : PictPort((Picture*)0)
{
	name= strsave(nm);
}

PrintPictPort::~PrintPictPort()
{
	FlushMyText();
	SafeDelete(name);
}

void PrintPictPort::DevOpenPage(int n)
{
	PictPort::DevOpenPage(n);
	pict= new Picture;
	pn= n;
}

void PrintPictPort::DevClosePage()
{
	PictPort::DevClosePage();
	if (pict->Length() > 0) {
		OStream os(form("%s.%d", name, pn));
		os << cMagic << " Picture PictPrinter\n" << pict NL;
	}
	SafeDelete(pict);
}

//---- PictPrinter -------------------------------------------------------------

class PictPrinter : public Printer {
public:
	MetaDef(PictPrinter);
	PictPrinter()
		{ }
	const char *GetName()
		{ return "PICT"; }
	bool CanSave()
		{ return TRUE; }
	PrintPort *MakePrintPort(const char *name)
		{ return new PrintPictPort(name); }
};

NewMetaImpl0(PictPrinter, Printer);

